#ifndef TABLEDELEGATES_H
#define TABLEDELEGATES_H

#include <QStyledItemDelegate>
#include <QComboBox>
#include <QDateEdit>
#include <QLabel>
#include <QBuffer>
#include <QPainter>
#include <QEvent>
#include <QMouseEvent>
#include <QFileDialog>

class ComboBoxDelegate : public QStyledItemDelegate
{
    Q_OBJECT
public:
    ComboBoxDelegate(QObject* parent = nullptr) : QStyledItemDelegate(parent) {}

    void setItems(const QStringList& items)
    {
        m_items = items; // 设置下拉菜单的选项
    }

    // 创建编辑器
    QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const override
    {
        Q_UNUSED(option);
        Q_UNUSED(index);
        QComboBox* editor = new QComboBox(parent);
        editor->addItems(m_items); // 添加选项到下拉菜单
        return editor;
    }

    // // 设置模型数据
    // void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const override
    // {
    //     QComboBox* comboBox = static_cast<QComboBox*>(editor);
    //     QString value = comboBox->currentText();
    //     model->setData(index, value, Qt::EditRole);
    // }

private:
    QStringList m_items; // 存储下拉菜单的选项
};

class DateEditDelegate : public QStyledItemDelegate {
public:
    explicit DateEditDelegate(QObject* parent = nullptr) : QStyledItemDelegate(parent) {}

    QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const override {
        QDateTimeEdit* editor = new QDateTimeEdit(parent);
        editor->setDisplayFormat("yyyy-MM-dd"); // 设置日期格式
        editor->setCalendarPopup(true);  // 启用日历弹出
        return editor;
    }

    // void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const override {
    //     QDateTimeEdit* dateEdit = qobject_cast<QDateTimeEdit*>(editor);
    //     if (dateEdit)
    //         model->setData(index, dateEdit->date().toString("yyyy-MM-dd"), Qt::EditRole);
    // }
};

class ImageDelegate : public QStyledItemDelegate {
public:
    explicit ImageDelegate(QObject* parent = nullptr) : QStyledItemDelegate(parent) {}

    QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const override {
        QLabel* editor = new QLabel(parent);
        return editor;
    }

    void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const override {
        QLabel* label = qobject_cast<QLabel*>(editor);
        if (label) {
            QByteArray imageData = index.data(Qt::UserRole).toByteArray();
            if (!imageData.isEmpty()) {
                QPixmap pixmap;
                if (pixmap.loadFromData(imageData) && !pixmap.isNull()) {
                    QBuffer buffer(&imageData);
                    buffer.open(QIODevice::WriteOnly);
                    pixmap.save(&buffer, "PNG");
                }
                model->setData(index, imageData, Qt::UserRole);
            }
        }
    }

    void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override {
        QByteArray imageData = index.data(Qt::UserRole).toByteArray();
        if (imageData.isEmpty()) {
            QStyledItemDelegate::paint(painter, option, index);
            return;
        }

        QPixmap pixmap;
        if (!pixmap.loadFromData(imageData) || pixmap.isNull()) {
            QStyledItemDelegate::paint(painter, option, index);
            return;
        }

        QRect rect = option.rect;
        QPixmap scaledPixmap = pixmap.scaled(rect.size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
        painter->drawPixmap(rect, scaledPixmap);
    }

    bool editorEvent(QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index) override {
        if (event->type() == QEvent::MouseButtonDblClick) {
            QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
            if (mouseEvent->button() == Qt::LeftButton) {
                QString imagePath = QFileDialog::getOpenFileName(nullptr, "选择图片", "", "图片文件 (*.png *.jpg *.jpeg *.bmp)");
                if (!imagePath.isEmpty()) {
                    QFile file(imagePath);
                    if (file.open(QIODevice::ReadOnly)) {
                        QByteArray imageData = file.readAll();
                        file.close();
                        model->setData(index, imageData, Qt::UserRole);
                    }
                }
                return true;
            }
        }
        return QStyledItemDelegate::editorEvent(event, model, option, index);
    }
};
#endif
